angular.module('app.baseinfo')
    .controller('emailCtrl', ['$scope', '$filter', '$modal', 'toaster', 'emailService',
        function ($scope, $filter, $modal, toaster, emailService) {

            $scope.getMailsettings = function () {
                emailService.getMailsettings().then(function (res) {
                    $scope.email = res.data.data;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };
            $scope.getMailsettings();

            $scope.updateMailsettings = function () {
                emailService.updateMailsettings($scope.email).then(function (res) {
                    toaster.pop("success", "成功", "更新成功");
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败");
                });
            };

            $scope.test = function () {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/baseinfo/email-modal.html',
                    controller: 'testEmailCtrl',
                    size: 'md',
                    resolve: {
                        email: function () {
                            return $scope.email;
                        }
                    }
                });
                modalInstance.result.then(function () {

                });
            };

            $scope.addEmail = function (type, emails) {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/baseinfo/add_email-modal.html',
                    controller: 'addEmailCtrl',
                    size: 'md',
                    resolve: {
                        emails: function () {
                            return emails;
                        },
                        type: function () {
                            return type;
                        }
                    }
                });
                modalInstance.result.then(function (res) {
                   switch(res.type){
                       case 1:
                           $scope.email.domesticSoMailReceivers=res.email;
                           break;
                       case 2:
                           $scope.email.abroadSoMailReceivers=res.email;
                           break;
                       case 3:
                           $scope.email.prsSoMailReceivers=res.email;
                           break;
                   }
                });
            }
        }])
    .controller('testEmailCtrl', ['$scope', '$filter', '$modalInstance', 'toaster', 'emailService', 'email',
        function ($scope, $filter, $modalInstance, toaster, emailService, email) {

            $scope.email = {};
            angular.copy(email, $scope.email);
            $scope.ok = function () {
                emailService.mailTest($scope.email).then(function (res) {
                    toaster.pop("success", "成功", "发送成功");
                    $modalInstance.close();
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "发送失败");
                });
            };

            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }])
    .controller('addEmailCtrl', ['$scope', '$filter', '$modalInstance', 'toaster', 'emailService', 'emails', 'type',
        function ($scope, $filter, $modalInstance, toaster, emailService, emails, type) {
            var emails = emails;
            var type = type;

            $scope.content={};
            if(emails==''){
                $scope.content.emails=[];
            }else{
                $scope.content.emails=emails.split(",");
            }


            $scope.addAddress=function(){
                if($scope.content.address==null||$scope.content.address.trim()==''){
                    return;
                }
                var index=$scope.content.emails.indexOf($scope.content.address);
                if (index > -1) {
                    toaster.pop("error", "错误",  "邮箱地址已存在");
                    return;
                }
                $scope.content.emails.push($scope.content.address);
            };

            $scope.deleteAddress=function(address){
                var index=$scope.content.emails.indexOf(address);
                if (index > -1) {
                    $scope.content.emails.splice(index, 1);
                }
            };

            $scope.ok=function(){
                var result={
                    "email":$scope.content.emails.join(","),
                    "type":type
                };
                $modalInstance.close(result);
            };

            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }]);